{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## while loop"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![title](images/3.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T08:44:00.780767Z",
     "start_time": "2018-08-21T08:44:00.773487Z"
    }
   },
   "outputs": [],
   "source": [
    "# count = 0\n",
    "# while(count < 3):\n",
    "#     print('The count is:  ', count)\n",
    "#     count = count + 1\n",
    "# print('Good bye! count is: ', count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T04:47:35.753801Z",
     "start_time": "2018-08-21T04:47:35.739837Z"
    }
   },
   "outputs": [],
   "source": [
    "# tup = (1,2,3,4,5)\n",
    "\n",
    "# prod = 1\n",
    "# index = 0\n",
    "\n",
    "# while(index < len(tup)):\n",
    "#     prod *= tup[index]\n",
    "#     index += 1\n",
    "\n",
    "# print('Product = {}'.format(prod))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### while....else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T08:48:22.005254Z",
     "start_time": "2018-08-21T08:48:21.998422Z"
    }
   },
   "outputs": [],
   "source": [
    "# tup = (1,2,3,4,5)\n",
    "\n",
    "# prod = 1\n",
    "# index = 0\n",
    "\n",
    "# while index < len(tup):\n",
    "#     prod *= tup[index]\n",
    "#     index += 1\n",
    "# else:\n",
    "#     print('No more numbers in tuple.')\n",
    "\n",
    "# print('Product = {}'.format(prod))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Membership Operators"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>in and not in</b> are membership operators in Python.\n",
    "\n",
    "Used to find whether a value or variable is found in a sequence."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T08:38:36.995305Z",
     "start_time": "2018-08-21T08:38:36.990755Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "lst = [1,2,3,4]\n",
    "\n",
    "print(5 in lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T08:39:14.574036Z",
     "start_time": "2018-08-21T08:39:14.567866Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "dict = {1:'a', 2:'b'}\n",
    "\n",
    "print('a' in dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T08:39:58.902962Z",
     "start_time": "2018-08-21T08:39:58.897579Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "s = 'Python'\n",
    "\n",
    "print('p' in s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# tup = (10, 20, 30, 40)\n",
    "\n",
    "# for x in tup:\n",
    "#     print(x)\n",
    "    \n",
    "# print('Finish')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T05:02:33.551659Z",
     "start_time": "2018-08-21T05:02:33.545688Z"
    }
   },
   "outputs": [],
   "source": [
    "# tup = (1,2,3,4,5)\n",
    "\n",
    "# prod = 1\n",
    "\n",
    "# for item in tup:\n",
    "#     prod *= item\n",
    "\n",
    "# print('Product = {}'.format(prod))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T09:25:03.146049Z",
     "start_time": "2018-08-21T09:25:03.141059Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The count is:   1\n",
      "The count is:   3\n",
      "The count is:   5\n",
      "The count is:   7\n",
      "The count is:   9\n",
      "Good bye!\n"
     ]
    }
   ],
   "source": [
    "for count in range(1, 10, 2):\n",
    "    print('The count is:  ', count)\n",
    "print('Good bye!')\n",
    "\n",
    "# try range(1, 10, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T05:10:06.781714Z",
     "start_time": "2018-08-21T05:10:06.774715Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "20\n",
      "30\n",
      "40\n",
      "50\n",
      "60\n"
     ]
    }
   ],
   "source": [
    "# Iterating directly element by element\n",
    "\n",
    "\n",
    "# Iterating by sequence index\n",
    "lst = [10,20,30,40,50,60]\n",
    "for index in range(len(lst)):\n",
    "    print(lst[index])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T05:15:12.102770Z",
     "start_time": "2018-08-21T05:15:12.093794Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LIST ITERATION: \n",
      "10 20 30 40 50 60 \n",
      "TUPLE ITERATION: \n",
      "10 20 30 40 50 60 \n",
      "STRING ITERATION: \n",
      "P y t h o n \n",
      "DICTIONARY ITERATION: \n",
      "xyz 123\n",
      "abc 345\n"
     ]
    }
   ],
   "source": [
    "print('LIST ITERATION: ')\n",
    "lst = [10,20,30,40,50,60]\n",
    "for element in lst:\n",
    "    print(element, end=' ')\n",
    "print()\n",
    "\n",
    "print('TUPLE ITERATION: ')\n",
    "tup = (10,20,30,40,50,60)\n",
    "for element in tup:\n",
    "    print(element, end=' ')\n",
    "print()\n",
    "\n",
    "print('STRING ITERATION: ')\n",
    "myStr = 'Python'\n",
    "for element in myStr:\n",
    "    print(element, end=' ')\n",
    "print()\n",
    "\n",
    "print('DICTIONARY ITERATION: ')\n",
    "dict = {}\n",
    "dict['xyz'] = 123\n",
    "dict['abc'] = 345\n",
    "for key in dict:\n",
    "    print(key, dict[key])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for.....else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T05:15:58.458431Z",
     "start_time": "2018-08-21T05:15:58.451450Z"
    }
   },
   "outputs": [],
   "source": [
    "# tup = (1,2,3,4,5)\n",
    "\n",
    "# prod = 1\n",
    "\n",
    "# for item in tup:\n",
    "#     prod *= item\n",
    "# else:\n",
    "#     print('You have exhausted the list')\n",
    "\n",
    "# print('Product = {}'.format(prod))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Nested Loops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T09:33:18.530097Z",
     "start_time": "2018-08-21T09:33:18.515138Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i: 0, j:0\n",
      "i: 0, j:1\n",
      "i: 0, j:2\n",
      "i: 1, j:0\n",
      "i: 1, j:1\n",
      "i: 1, j:2\n",
      "i: 2, j:0\n",
      "i: 2, j:1\n",
      "i: 2, j:2\n",
      "i: 3, j:0\n",
      "i: 3, j:1\n",
      "i: 3, j:2\n",
      "i: 4, j:0\n",
      "i: 4, j:1\n",
      "i: 4, j:2\n",
      "i: 5, j:0\n",
      "i: 5, j:1\n",
      "i: 5, j:2\n",
      "i: 6, j:0\n",
      "i: 6, j:1\n",
      "i: 6, j:2\n",
      "i: 7, j:0\n",
      "i: 7, j:1\n",
      "i: 7, j:2\n",
      "i: 8, j:0\n",
      "i: 8, j:1\n",
      "i: 8, j:2\n",
      "i: 9, j:0\n",
      "i: 9, j:1\n",
      "i: 9, j:2\n",
      "I am outside the while loop\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "\n",
    "while i < 10:\n",
    "    j = 0\n",
    "    while j < 3:\n",
    "        print('i: {}, j:{}'.format(i, j))\n",
    "        j += 1\n",
    "        \n",
    "    i += 1\n",
    "    \n",
    "print('I am outside the while loop')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BREAK & CONTINUE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-27T04:06:33.272402Z",
     "start_time": "2018-05-27T04:06:33.265224Z"
    }
   },
   "source": [
    "![title](images/1.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T09:39:51.372454Z",
     "start_time": "2018-08-21T09:39:51.357455Z"
    }
   },
   "outputs": [],
   "source": [
    "# for count in range(10):\n",
    "#     if count == 5:\n",
    "#         break\n",
    "#     print('The count is:  ', count)\n",
    "# print('Good bye!')\n",
    "# # try else"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![title](images/2.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T09:40:37.730629Z",
     "start_time": "2018-08-21T09:40:37.724644Z"
    },
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# for count in range(10):\n",
    "#     if count == 5:\n",
    "#         continue\n",
    "#     print(count)\n",
    "# print('Good bye!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-21T09:41:31.678539Z",
     "start_time": "2018-08-21T09:41:24.348848Z"
    }
   },
   "outputs": [],
   "source": [
    "# while True:\n",
    "#     num = int(input(\"Enter a number to check if its even or odd: \"))\n",
    "#     if num % 2 == 0:\n",
    "#         print('Congratulations, you entered an even number.')\n",
    "#     else:\n",
    "#         break\n",
    "# print('Wrong Number, DUMBO!!\\\\nGAME OVER')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Why else?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# lst = [1,2,3,4]\n",
    "\n",
    "# for item in lst:\n",
    "#     if item == 3:\n",
    "#         break\n",
    "#     print(item)\n",
    "# else:\n",
    "#     print('huraaaay!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
